package Work2;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.IOException;

public class SerializeUser {
    public static void main(String[] args) {
        String name = System.console().readLine("请输入用户名: ");
        String pwd = System.console().readLine("请输入密码: ");
        String nick = System.console().readLine("请输入昵称: ");
        int age = Integer.parseInt(System.console().readLine("请输入年龄: "));

        User user = new User(name, pwd, nick, age);

        try (FileOutputStream fos = new FileOutputStream(name + ".obj");
             ObjectOutputStream oos = new ObjectOutputStream(fos)) {
            oos.writeObject(user);
            System.out.println("用户信息已成功序列化到文件: " + name + ".obj");
        } catch (IOException e) {
            System.err.println("序列化过程中发生错误: " + e.getMessage());
        }
    }
}
